package wechat.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.alibaba.fastjson.JSONObject;

import wechat.resources.AppFilter;
import wechat.resources.InstanceResource;
import wechat.service.impl.IUserService;
import wechat.util.StreamUtil;

import java.io.InputStream;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Controller
@RequestMapping("/user")
public class UserController {

	private Logger log = LoggerFactory.getLogger(UserController.class);

	@Autowired
	IUserService userService;
	
	/**
	 * 
	 * 
	 *	    发送的数据形式。特别注意，数据请放在body里发送。
	 *   推荐使用com.ryt.wechat.util.http包下的HttpClientUtil类中的post方法
	 * 
	 * 数据形式：
	 * {
	 *	   "userid": "zhangsan",
	 *	   "name": "张三",
	 *	   "department": [1, 2],
	 *	   "position": "产品经理",
	 *	   "mobile": "15913215421",
	 *	   "gender": "1",
	 *	   "email": "zhangsan@gzdev.com",
	 *	   "weixinid": "zhangsan4dev",
	 *	   "avatar_mediaid": "2-G6nrLmr5EC3MNb_-zL1dDdzkd0p7cNliYu9V5w7o8K0",
	 *	   "extattr": {"attrs":[{"name":"爱好","value":"旅游"},{"name":"卡号","value":"1234567234"}]}
	 *	}
	 * 
	 *  
	 *  具体含义参照微信文档：http://qydev.weixin.qq.com/wiki/index.php?title=%E7%AE%A1%E7%90%86%E6%88%90%E5%91%98
	 *  
	 * 
	 * @param request
	 * @param response
	 * @param appid
	 * @return
	 */
	@RequestMapping(value = "/createUser/{appid}", method = RequestMethod.POST,produces = "application/json; charset=utf-8")
	@ResponseBody
	public String createUser(HttpServletRequest request, HttpServletResponse response,
			@PathVariable(value = "appid") String appid) {

		InstanceResource instanceResource = AppFilter.chooseApp(appid);
		String result = "";
		
		InputStream input;
		try {
			input = request.getInputStream();

			String str = StreamUtil.inputStreamToString(input);
			log.info("UserController -- createUser  str=" + str);

			JSONObject jsonParam = JSONObject.parseObject(str);
			log.info("UserController -- createUser  jsonParam=" + jsonParam);
			
			result = userService.createUser(instanceResource, jsonParam);
			log.info("UserController -- createUser  result=" + result);
		} catch (Exception e) {
			log.error("UserController -- createUser  失败", e);
			e.printStackTrace();
		}

		return result;
	}
	
	
	/**
	 * 
	 * 
	 *	    发送的数据形式。特别注意，数据请放在body里发送。
	 *   推荐使用com.ryt.wechat.util.http包下的HttpClientUtil类中的post方法
	 * 
	 * 数据形式：
	 * {
	 *	   "userid": "zhangsan",
	 *	   "name": "张三",
	 *	   "department": [1, 2],
	 *	   "position": "产品经理",
	 *	   "mobile": "15913215421",
	 *	   "gender": "1",
	 *	   "email": "zhangsan@gzdev.com",
	 *	   "weixinid": "zhangsan4dev",
	 *	   "avatar_mediaid": "2-G6nrLmr5EC3MNb_-zL1dDdzkd0p7cNliYu9V5w7o8K0",
	 *	   "extattr": {"attrs":[{"name":"爱好","value":"旅游"},{"name":"卡号","value":"1234567234"}]}
	 *	}
	 * 
	 *  
	 *  具体含义参照微信文档：http://qydev.weixin.qq.com/wiki/index.php?title=%E7%AE%A1%E7%90%86%E6%88%90%E5%91%98
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/updateUser/{appid}", method = RequestMethod.POST,produces = "application/json; charset=utf-8")
	@ResponseBody
	public String updateUser(HttpServletRequest request, HttpServletResponse response,
			@PathVariable(value = "appid") String appid) {

		InstanceResource instanceResource = AppFilter.chooseApp(appid);
		String result = "";
		
		InputStream input;
		try {
			input = request.getInputStream();

			String str = StreamUtil.inputStreamToString(input);
			log.info("UserController -- updateUser  str=" + str);

			JSONObject jsonParam = JSONObject.parseObject(str);
			log.info("UserController -- updateUser  jsonParam=" + jsonParam);
			
			result = userService.updateUser(instanceResource, jsonParam);
			log.info("UserController -- updateUser  result=" + result);
		} catch (Exception e) {
			log.error("UserController -- updateUser  失败", e);
			e.printStackTrace();
		}

		return result;
	}
	
	
	@RequestMapping(value = "/deleteUser/{appid}", method = RequestMethod.GET,produces = "application/json; charset=utf-8")
	@ResponseBody
	public String deleteUser(HttpServletRequest request, HttpServletResponse response,
			@PathVariable(value = "appid") String appid,
			@RequestParam(value = "userid") String userid) {

		InstanceResource instanceResource = AppFilter.chooseApp(appid);
		String result = "";

		try {
			result = userService.deleteUser(instanceResource, userid);
			log.info("UserController -- deleteUser  result=" + result);
		} catch (Exception e) {
			log.error("UserController -- deleteUser  失败", e);
			e.printStackTrace();
		}

		return result;
	}
	
	
	
	
	/**
	 * 
	 *   发送的数据形式。特别注意，数据请放在body里发送。
	 *   推荐使用com.ryt.wechat.util.http包下的HttpClientUtil类中的post方法
	 * 
	 * 数据形式：
	 *		{
	 *	      "useridlist": ["zhangsan", "lisi"]
	 *	    }
	 * 
	 *  
	 *  具体含义参照微信文档：http://qydev.weixin.qq.com/wiki/index.php?title=%E7%AE%A1%E7%90%86%E6%88%90%E5%91%98
	 * 
	 * 
	 * 
	 * 
	 * @param request
	 * @param response
	 * @param appid
	 * @return
	 */
	@RequestMapping(value = "/batchdeleteUser/{appid}", method = RequestMethod.POST,produces = "application/json; charset=utf-8")
	@ResponseBody
	public String batchdeleteUser(HttpServletRequest request, HttpServletResponse response,
			@PathVariable(value = "appid") String appid) {

		InstanceResource instanceResource = AppFilter.chooseApp(appid);
		String result = "";
		
		InputStream input;
		try {
			input = request.getInputStream();

			String str = StreamUtil.inputStreamToString(input);
			log.info("UserController -- batchdeleteUser  str=" + str);

			JSONObject jsonParam = JSONObject.parseObject(str);
			log.info("UserController -- batchdeleteUser  jsonParam=" + jsonParam);
			
			result = userService.batchdeleteUser(instanceResource, jsonParam);
			log.info("UserController -- batchdeleteUser  result=" + result);
		} catch (Exception e) {
			log.error("UserController -- batchdeleteUser  失败", e);
			e.printStackTrace();
		}

		return result;
	}


	@RequestMapping(value = "/getUser/{appid}", method = RequestMethod.GET,produces = "application/json; charset=utf-8")
	@ResponseBody
	public String getUser(HttpServletRequest request, HttpServletResponse response,
			@PathVariable(value = "appid") String appid,
			@RequestParam(value = "userid") String userid) {

		InstanceResource instanceResource = AppFilter.chooseApp(appid);
		String result = "";

		try {
			result = userService.getUser(instanceResource, userid);
			log.info("UserController -- getUser  result=" + result);
		} catch (Exception e) {
			log.error("UserController -- getUser  失败", e);
			e.printStackTrace();
		}

		return result;
	}
	
	
	@RequestMapping(value = "/simplelistUser/{appid}", method = RequestMethod.GET,produces = "application/json; charset=utf-8")
	@ResponseBody
	public String simplelistUser(HttpServletRequest request, HttpServletResponse response,
			@PathVariable(value = "appid") String appid,
			@RequestParam(value = "departmentid") String departmentid,
			@RequestParam(value = "fetchchild", required = false) String fetchchild,
			@RequestParam(value = "status", required = false) String status) {

		InstanceResource instanceResource = AppFilter.chooseApp(appid);
		String result = "";

		try {
			result = userService.simplelistUser(instanceResource, departmentid, fetchchild, status);
			log.info("UserController -- simplelistUser  result=" + result);
		} catch (Exception e) {
			log.error("UserController -- simplelistUser  失败", e);
			e.printStackTrace();
		}

		return result;
	}
	
	
	@RequestMapping(value = "/listUser/{appid}", method = RequestMethod.GET,produces = "application/json; charset=utf-8")
	@ResponseBody
	public String listUser(HttpServletRequest request, HttpServletResponse response,
			@PathVariable(value = "appid") String appid,
			@RequestParam(value = "departmentid") String departmentid,
			@RequestParam(value = "fetchchild", required = false) String fetchchild,
			@RequestParam(value = "status", required = false) String status) {

		InstanceResource instanceResource = AppFilter.chooseApp(appid);
		String result = "";

		try {
			result = userService.listUser(instanceResource, departmentid, fetchchild, status);
			log.info("UserController -- listUser  result=" + result);
		} catch (Exception e) {
			log.error("UserController -- listUser  失败", e);
			e.printStackTrace();
		}

		return result;
	}
}
